<template>
  <div class="channel-edit">
    <van-cell>
      <div
        slot="title"
        class="title-text"
      >
        我的频道
      </div>
      <van-button
        size="mini"
        plain
        type="danger"
        round
        class="edit-btn"
        @click="isEdit = !isEdit"
      >
        {{isEdit?'完成':'编辑'}}
      </van-button>
    </van-cell>
    <van-grid
      :gutter="10"
      column-num="4"
      class="my-grid"
    >
      <van-grid-item
        v-for="(channel, index) in myChannels"
        :key="index"
        class="grid-item"
        :class="{isEdit: isEdit && !fixedChannels.includes(channel.id)}"
        icon="clear"
        @click="onMyChannelClick(channel, index)"
      >
        <span
          slot="text"
          class="text"
          :class="{
            active: index === activeIndex
          }"
        >{{ channel.name }}</span>
      </van-grid-item>
    </van-grid>
    <van-cell>
      <div
        slot="title"
        class="title-text"
      >
        频道推荐
      </div>
    </van-cell>
    <van-grid
      :gutter="10"
      column-num="4"
      class="recommend-grid"
    >
      <van-grid-item
        v-for="(channel,index) in recommendChannels"
        :key="index"
        :text="channel.name"
        icon="plus"
        class="grid-item"
        @click="addItem(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import { addChannel, deleteChannelById, getAllChannels } from '@/api/channels.js'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage'
export default {
  name: 'ChannelEdit',
  components: {},
  props: {
    myChannels: {
      type: Array,
      required: true
    },
    activeIndex: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      fixedChannels: [0], // 不能删除的频道id
      isEdit: false // 是否处于编辑状态
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      return this.allChannels.filter(t => {
        const f = this.myChannels.find(m => t.id === m.id)
        return !f
      })
    }
  },
  async created () {
    const { data: { data: { channels } } } = await getAllChannels()
    this.allChannels = channels
  },
  methods: {
    async addItem (channel) {
      // 因为是数组,地址所有vue没办法阻止
      this.myChannels.push(channel)
      // 已登录,发请求
      if (this.user) {
        await addChannel({ id: channel.id, seq: this.myChannels.length })
      } else {
        // 未登录,存本地
        setItem('TOUTIAO_CHANNELS', this.myChannels)
      }
    },
    async onMyChannelClick (channel, index) {
      if (this.isEdit) {
        // 删除操作
        if (this.fixedChannels.includes(channel.id)) {
          return
        }
        if (index <= this.activeIndex) {
          this.$emit('update-active', this.activeIndex - 1)
        }
        this.myChannels.splice(index, 1)

        // 已登录,发请求
        if (this.user) {
          await deleteChannelById(channel.id)
        } else {
        // 未登录,存本地
          setItem('TOUTIAO_CHANNELS', this.myChannels)
        }
      } else {
        // 切换操作
        this.$emit('update-active', index, false)
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .van-icon-clear{
  display: none;
}
/deep/ .isEdit .van-icon-clear{
  display: block;
}
.channel-edit {
  padding: 85px 0;
}
.title-text {
  font-size: 32px;
  color: #333333;
}
.edit-btn {
  width: 104px;
  height: 48px;
  font-size: 26px;
  color: #f85959;
  border: 1px solid #f85959;
}
/deep/ .grid-item {
  width: 160px;
  height: 86px;
  .van-grid-item__content {
    white-space: nowrap;
    background-color: #f4f5f6;
    .van-grid-item__text,
    .text {
      font-size: 28px;
      color: #222;
      margin-top: 0;
    }
    .active{
        color: red
    }
  }
}
/deep/ .my-grid {
  .grid-item {
    .van-icon-clear {
      position: absolute;
      right: -10px;
      top: -10px;
      font-size: 30px;
      color: #cacaca;
      z-index: 2;
    }
  }
}
/deep/ .recommend-grid {
  .grid-item {
    .van-grid-item__content {
      flex-direction: row;
      .van-icon-plus {
        font-size: 28px;
        margin-right: 6px;
      }
    }
  }
}
</style>
